///////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2010 Chris Colinsky
// All Rights Reserved
//
// version	0.5
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

package sugarcookie.utils {
	import flash.utils.getQualifiedClassName;
	
	import mx.utils.UIDUtil;
	
	public class StringUtil {
		public static function replaceAll(pattern:RegExp,str:String,repl:String):String {
			str = str.replace(pattern, patternSearch);
			
			function patternSearch():String {
				return repl;
			}
			return str;
		}
		
		public static function getFormattedClassName(object:*,fullName:Boolean=false):String {
			var qname:String = getQualifiedClassName(object);
			return fullName?qname.replace("::",".") : qname.substr(qname.lastIndexOf("::")+2);
		}
		
		public static function wordCount(str:String):Object {
			var pattern:RegExp = /\b[\w(-\w)?]+\b/g;
			var result:Array = str.match(pattern);
			return {length:result.length, words:result};
		}
		
		public static function initialCaps(word:String):String {
			var str:String = word.slice(0,1).toUpperCase() + word.slice(1);
			return str;
		}
		
		public static function formatTimecode(number:int):String {
			var min:Number = 0;
			var sec:Number = 0;
			
			if (number>59) {
				min = int(number/60);
				sec = int(number % 60);
			} else {
				sec = number;
			}
			
			var timecode:String = "";
			timecode += min<10?addPadding(min):min;
			timecode += ":";
			timecode += sec<10?addPadding(sec):sec;
			return timecode;
		}
		
		public static function addPadding(number:uint,padding:Number=1):String {
			var num:String = "";
			for (var i:int=0;i<padding;i++) {
				num += "0";
			}
			return num += String(number);
		}
		
		public static function createUUID():String {
			return UIDUtil.createUID();
		}
	}
}